import { segment } from "oicq"
import fetch from 'node-fetch'
import moment from "moment"

//插件作者：煌（2608259582） 于2022/01/17编写，请勿大范围传播，首发摆烂群（891838593）.
//转发信息文案位于127-158行，如有需要，请自行修改文案.

const Botname = global.Bot.nickname //Bot名字，可自行修改.
const BotQQ = global.Bot.uin //想要显示的账号，可自行修改.

export class BindingHelp extends plugin {
	constructor() {
		super( {
			name: "绑定帮助",
			dsc: "绑定帮助",
			event: "message",
			priority: 1,
			rule: [ {
				reg: "^#?(绑|绑定|米游社|米哈游)?(绑定|体力|米游币|米游社|ck|cookie|Cookie|cookies|Cookies|sk|stoken|Stoken)(帮助|教程|攻略)$",
				fnc: "BindingHelp"
			} ]
		} )
	}

	async BindingHelp ( e ) {
		let Version = 3.4
		let baseTime = new Date( '2023-1-18 11:00:00' )
			.getTime()
		let nowTime = new Date()
			.getTime()
		let duringTime = baseTime - nowTime
		while ( duringTime <= 0 ) {
			duringTime += 42 * 24 * 60 * 60 * 1000
			Version += 0.1
		}
		let days = Math.floor( duringTime / ( 24 * 3600 * 1000 ) )
		let leave1 = duringTime % ( 24 * 3600 * 1000 );
		let hours = Math.floor( leave1 / ( 3600 * 1000 ) );
		let leave2 = leave1 % ( 3600 * 1000 );
		let minutes = Math.floor( leave2 / ( 60 * 1000 ) );
		let now = new Date()
		let hour = now.getHours()
		let minutes2 = now.getMinutes()
		if ( hour < 10 ) {
			hour = `0${hour.toString()}`
		}
		if ( minutes < 10 ) {
			minutes = `0${minutes.toString()}`
		}
		let res = await fetch( 'https://v1.hitokoto.cn/' )
			.catch( ( err ) => logger.error( err ) )
		if ( !res ) {
			return false
		}
		res = await res.json()
		let month = Number( moment()
			.month() ) + 1
		let monthKey = `Yz:count:sendMsg:month:`
		let messageCount = await redis.get( `${monthKey}${month}` ) || 0
		let BigHourName = [ '子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥', '子' ]
		let BigHourType = [ '正', '初' ]
		let BigMinName = [ '零', '一', '二', '三', '四' ]
		let hour2 = Number( now.getHours() )
		let bot1
		if ( e.isPrivate ) {
			bot1 = {
				nickname: `${Botname} | 离原神${Version.toFixed(1)}还有${days}天${hours}小时${minutes}分钟.`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		} else {
			bot1 = {
				nickname: `${Botname} | 离原神${Version.toFixed(1)}还有${days}天${hours}小时${minutes}分钟.`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		}
		let bot2
		if ( e.isPrivate ) {
			bot2 = {
				nickname: `${Botname} | 现在是北京时间${hour}:${minutes2}`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		} else {
			bot2 = {
				nickname: `${Botname} | 现在是北京时间${hour}:${minutes2}`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		}
		let bot3
		if ( e.isPrivate ) {
			bot3 = {
				nickname: `${Botname} | ${res.hitokoto}`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		} else {
			bot3 = {
				nickname: `${Botname} | ${res.hitokoto}`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		}
		let bot4
		if ( e.isPrivate ) {
			bot4 = {
				nickname: `${Botname} | 本月已发送${messageCount}条消息.`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		} else {
			bot4 = {
				nickname: `${Botname} | 本月已发送${messageCount}条消息.`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		}
		let bot5
		if ( e.isPrivate ) {
			bot5 = {
				nickname: `${Botname} | 现在是长安${BigHourName[Math.floor((hour2 + 1)/2)]}${BigHourType[hour % 2]}${BigMinName[Math.floor(minutes2/15)]}刻`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		} else {
			bot5 = {
				nickname: `${Botname} | 现在是长安${BigHourName[Math.floor((hour2 + 1)/2)]}${BigHourType[hour % 2]}${BigMinName[Math.floor(minutes2/15)]}刻`,
				user_id: BotQQ
			} //user_id为QQ号，nickname为名称，可修改为与账号不同的名称.
		}
		let MsgList = []
		let urls_one = `https://i.imgtg.com/2023/01/12/Qkfqx.jpg` //访问一下图床地址
		let urls_two = `https://i.imgtg.com/2023/01/12/QkjCU.jpg` //访问一下图床地址
		MsgList.push( {
			message: [
				'如果已经绑定过stoken了.', '\n',
				`对${Botname}发送【#刷新ck】即可.`
			],
			...bot1,
		}, {
			message: [
				'米游社扫码登录（推荐）.'
			],
			...bot2,
		}, {
			message: [
				`1.私聊${Botname}发送【#扫码登录】.`, '\n',
				`2.将${Botname}发送的二维码保存到本地.`, '\n',
				'3.打开米游社扫码登录选择【相册】，扫码登录即可.', '\n',
				segment.image( urls_one ),
				segment.image( urls_two )
			],
			...bot3,
		}, {
			message: [
				'米游社账密登录（不推荐）.'
			],
			...bot4,
		}, {
			message: [
				`1.私聊${Botname}发送【#账密登录】.`, '\n',
				'2.随后按提示发送账号及密码即可.'
			],
			...bot5,
		}, )
		let execute
		if ( e.isGroup )
			execute = await e.reply( [ await e.group.makeForwardMsg( MsgList ) ] )
		else
			execute = await e.reply( [ await e.friend.makeForwardMsg( MsgList ) ] )
		console.log( "执行完毕." )
		return true
	}
}
